#include <sys/types.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "db.h"

#define	DATABASE  "access.db"
#define	DATABASE2 "recno.db"

int
recno_build (dbp)
	DB *dbp;
{
	DBC *dbcp;
	DBT key, data;
	db_recno_t recno;
	u_int32_t len;
	int ret;
	char buf[1024];

	/* Insert records into the database. */
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	for (recno = 1; recno < 5; ++recno) {
		printf("record #%lu> ", (u_long)recno);
		fflush(stdout);
		if (fgets(buf, sizeof(buf), stdin) == NULL)
			break;
		if ((len = strlen(buf)) <= 1)
			continue;

//		key.data = &recno;
//		key.size = sizeof(recno);
		data.data = buf;
		data.size = len - 1;

//		switch (ret = dbp->put(dbp, NULL, &key, &data, 0)) {
		switch (ret = dbp->put(dbp, NULL, &key, &data, DB_APPEND)) {			
		case 0:
			break;
		default:
			dbp->err(dbp, ret, "DB->put");
			break;
		}
	}
	printf("\n");

	/* Acquire a cursor for the database. */
	if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
		dbp->err(dbp, ret, "DB->cursor");
		return (1);
	}

	/* Re-initialize the key/data pair. */
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));

	/* Walk through the database and print out the key/data pairs. */
	while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0)
		printf("%lu : %.*s\n",
		    *(u_long *)key.data, (int)data.size, (char *)data.data);
	if (ret != DB_NOTFOUND)
		dbp->err(dbp, ret, "DBcursor->get");

	/* Close the cursor. */
	if ((ret = dbcp->c_close(dbcp)) != 0) {
		dbp->err(dbp, ret, "DBcursor->close");
		return (1);
	}
	return (0);
}

int
main()
{
	DB *dbp;
	//DBT key, data;
	int ret, t_ret;
	//db_recno_t recno;
	//u_int32_t countp;

	/* Create the database handle and open the underlying database. */
	if ((ret = db_create(&dbp, NULL, 0)) != 0) {
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
		exit (1);
	}
/*	if ((ret =
	    dbp->open(dbp, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
		dbp->err(dbp, ret, "%s", DATABASE);
		goto err;
	}*/
	if ((ret =
	    dbp->open(dbp, NULL, DATABASE2, NULL, DB_RECNO, DB_CREATE, 0664)) != 0) {
		dbp->err(dbp, ret, "open %s", DATABASE);
		goto err;
	}
	
//	dbp->truncate (dbp, NULL, &countp, 0);
//	printf ("removed %d points\n", countp);

	recno_build (dbp);

	/* Initialize key/data structures. */
//	memset(&key, 0, sizeof(key));
//	memset(&data, 0, sizeof(data));
	
//	key.data = "fruit";
//	key.size = sizeof("fruit");

/*	recno = 10;
	key.data = &recno;
	key.size = sizeof(recno);

	data.data = "apple";
	data.size = sizeof("apple");

	/* Store a key/data pair. */
/*	if ((ret = dbp->put(dbp, NULL, &key, &data, DB_APPEND)) == 0)
		printf("db: %s: key stored.\n", (char *)key.data);
	else {
		dbp->err(dbp, ret, "DB->put");
		goto err;
	}

	/* Retrieve a key/data pair. */
/*	if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
		printf("db: %s: key retrieved: data was %s.\n",
		    (char *)key.data, (char *)data.data);
	else {
		dbp->err(dbp, ret, "DB->get");
		goto err;
	}

	printf ("key = %d\n", *((int*) key.data));
	/* Delete a key/data pair. */
/*	if ((ret = dbp->del(dbp, NULL, &key, 0)) == 0)
		printf("db: %s: key was deleted.\n", (char *)key.data);
	else {
		dbp->err(dbp, ret, "DB->del");
		goto err;
	}

	/* Retrieve a key/data pair. */
/*	if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
		printf("db: %s: key retrieved: data was %s.\n",
		    (char *)key.data, (char *)data.data);
	else
		dbp->err(dbp, ret, "DB->get");
*/
err:	if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
		ret = t_ret;

	exit(ret);
}
